package com.anpai.main;



import com.anpai.entity.UserBean;
import org.apache.poi.ss.usermodel.*;


import javax.swing.*;
import java.awt.*;
import java.awt.Color;

import java.io.File;
import java.io.FileInputStream;

import java.util.ArrayList;




public class adminFrame extends JFrame{
    private static String xlsPath;
    public static String path = System.getProperty("user.dir");

    public adminFrame(){
        this.setTitle("考试系统-管理员");
        setLocation(10, 10);
        setBounds(550,300, 800, 540);
        setLayout(new BorderLayout(10,10)); // 上下左右间距10

        // 顶部部分
        JPanel headJPanel = new JPanel();


        headJPanel.setLayout(new GridBagLayout());

        add(headJPanel, BorderLayout.NORTH);

        // 中间部分
        JPanel centerJPanel = new JPanel();

        JLabel jl = new JLabel("<html><h1>学生信息</h1></html>",SwingConstants.CENTER);

        JTextArea jTextArea = new JTextArea(20,65);
        jTextArea.setFont(new java.awt.Font("Dialog", 1, 15));
        jTextArea.setLineWrap(true);        //激活自动换行功能
        jTextArea.setWrapStyleWord(true);            // 激活断行不断字功能
        jTextArea.setEditable(false);
        ArrayList<UserBean> userBeans = readUser();
        String txtMessage="";
        for (UserBean user :userBeans) {
            txtMessage =txtMessage+user.getUserId()+"\t"+user.getName()+"\t"+user.getStudentNo()+"\t\t"+user.getResult()+"\n";
        }

        jTextArea.append(txtMessage);
        JScrollPane scroll = new JScrollPane(jTextArea);
        //把定义的JTextArea放到JScrollPane里面去
        //分别设置水平和垂直滚动条自动出现
        scroll.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        centerJPanel.add(jl);
        centerJPanel.add(scroll);
        add(centerJPanel);

        JPanel bottomJPanel = new JPanel();
        bottomJPanel.setBackground(Color.CYAN);


        add(bottomJPanel, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }


    public ArrayList<UserBean> readUser() {
        ArrayList<UserBean> userBeans = new ArrayList<>();

        try {
            xlsPath = path + "\\src\\main\\java\\com\\anpai\\data\\user.xls";
            File excelFile = new File(xlsPath); //创建文件对象
            FileInputStream is = new FileInputStream(excelFile); //文件流
            Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
            int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量
            //遍历每个Sheet
            for (int s = 0; s < sheetCount; s++) {
                Sheet sheet = workbook.getSheetAt(s);
                int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
                //遍历每一行
                for (int r = 0; r < rowCount; r++) {
                    Row row = sheet.getRow(r);
                    int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
                    //遍历每一个单元格
                    UserBean userBean = new UserBean();
                    for (int c = 0; c < cellCount; c++) {
                        Cell cell = row.getCell(c);
                        cell.getCellType();
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String cellValue = cell.getStringCellValue();
                        if (c == 0)
                           userBean.setUserId(cellValue);
                        else if (c == 1)
                            userBean.setUserName(cellValue);
                        else if (c==2)
                            userBean.setPassword(cellValue);
                        else if (c == 3)
                            userBean.setName(cellValue);
                        else if (c==4)
                            userBean.setStudentNo(cellValue);
                        else if (c==5)
                            userBean.setMajor(cellValue);
                        else if (c==6){
                            userBean.setClassRoom(cellValue);
                        }else
                            userBean.setResult(cellValue);

                    }
                    userBeans.add(userBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
return userBeans;
    }
}
